Prev Next |
Many of the Sitecore field types can be used to select media items; various forms are used to store references in the XML repository. The table below provides a summary of the features supported by various field types:
Field Type |
Multiple |
Order |
Upload |
Links |
Source |
Query |
Storage |
HTML |
True |
True |
True |
True |
N/A |
N/A |
Markup (HTML) |
Lookup |
False |
N/A |
False |
True |
True |
True |
GUID |
Reference |
False |
N/A |
False |
True |
True |
N/A |
GUID |
Tree |
False |
N/A |
False |
False |
True |
N/A |
GUID |
Checklist |
True |
False |
False |
True |
True |
True |
Pipe-separated GUID(s) |
Multilist |
True |
True |
False |
True |
True |
True |
Pipe-separated GUID(s) |
Image |
False |
N/A |
True |
True |
True |
N/A |
XML (file path, Sitecore path relative to media library, mediaid, etc) |
Link |
False |
N/A |
True |
True |
True |
N/A |
XML (Sitecore path relative to media library, id, etc.) |
InternalLink |
False |
N/A |
False |
True |
True |
N/A |
Full Sitecore path |
File |
False |
N/A |
True |
False |
True |
N/A |
Sitecore path relative to media library |
Treelist |
True |
True |
False |
False |
True |
N/A |
Pipe-separated GUID(s) |
- Field Type – the name of a Sitecore field type
- Multiple – allows the user to select multiple items
- Order – allows the user to order the selected items
- Upload – allows the user to upload new items (field types which don’t support upload require the user to navigate to media library and upload, then navigate back to content to reference the uploaded media)
- Links – references in fields of this type are maintained in Sitecore’s link database
- Source – supports The Source Property to limit selection to a specific region of the XML repository
- Query – field type supports query in Source as opposed to just a path
- Storage – indicates the format of references stored in this type of field
HTML
HTML fields may contain standard HTML anchors (href) to media as well as embedded media such as images (src); such references are tracked in the link database. In general such soft references should not be managed programmatically.
Selection
Lookup, Reference and Tree fields store the Sitecore GUID of the selected item.
Retrieving the path to the media item specified in a selection field is relatively straightforward – retrieve the Sitecore item corresponding to the GUID, then retrieve the path field from that item. Assuming the context item contains a field named SelectedItem the XSL code to retrieve the path would be as follows:
<xsl:variable name="mediaid" select="sc:fld( 'selecteditem', . )" />
<xsl:variable name="mediaitem" select="sc:item( $mediaid, . )" />
<sc:text field="path" select="$mediaitem" />
Abbreviated:
<sc:text field="path" select=" select="sc:item( sc:fld( 'selecteditem', . ), . )" />
The .NET equivalent:
string id = Sitecore.Context.Item.Fields["selecteditem"].Value;
Sitecore.Data.Items.Item itm = Sitecore.Context.Item.Database.Items[id];
string path = itm.Fields["path"].Value;
Abbreviated:
Sitecore.Context.Item.Database.Items[Sitecore.Context.Item.Fields["selecteditem"].Value].Fields["path"].Value;
Multiselect
Checklist and Multilist fields store the Sitecore GUIDs of the selected items separated by pipes (“|”) with no trailing pipes. A common solution is recursion using a named template. Notice the pipe appended to the definition of $guids to simplify string parsing:
<xsl:call-template name="NamedTemplate" />
...
<xsl:template name="NamedTemplate">
<xsl:param name="guids" select="concat( sc:fld( 'selecteditem', $home ), '|' )" />
<xsl:variable name="guid" select="substring-before( $guids, '|' )" />
<xsl:if test="$guid">
<xsl:variable name="item" select="sc:item( $guid, . )" />
<xsl:if test="$item">
<sc:text field="path" select="$item" />
</xsl:if>
<xsl:if test="substring-after( $guids, '|' )">
<xsl:call-template name="NamedTemplate">
<xsl:with-param name="guids" select="substring-after( $guids, '|' )" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
And the equivalent .NET:
Sitecore.Data.Fields.MultilistField mFld = Sitecore.Context.Item.Fields["multilist"];
foreach( Sitecore.Data.ID id in mFld.TargetIDs ) {
Sitecore.Data.Items.Item itm = Sitecore.Context.Item.Database.Items[id];
if ( itm != null ) {
// process itm.Fields["path"].Value
}
}
Image
Fields of type Image can actually be used for any media, storing their contents as follows:
<image showineditor="1" mediaid="{D50FA8A7-9872-4791-B40A-18D8D85DEDBF}" mediapath="/PDFs/Subdir/test pdf" src="/SdnArchive/upload/pdfs/subdir/test.pdf" alt="" />
Properties can be accessed with sc:fld:
<xsl:value-of select="sc:fld( 'selectedimage', ., 'src' )" />
Or the .NET equivalent:
Sitecore.Data.Fields.ImageField iFld = Sitecore.Context.Item.Fields["selectedimage"];
// process iFld.Src
Link
When a media item is selected in a field of type Link, content is stored as follows:
<link linktype="media" url="/PDFs/Subdir/test pdf" target="" id="{D50FA8A7-9872-4791-B40A-18D8D85DEDBF}" />
In XSL, sc:fld would be used to access the properties:
<xsl:variable name="mediaid" select="sc:fld( 'selecteditem', ., 'id' )" />
<xsl:variable name="mediaitem" select="sc:item( $mediaid, . )" />
<sc:text field="path" select="$mediaitem" />
Or abbreviated:
<sc:text field="path" select=" select="sc:item( sc:fld( 'selecteditem', ., 'id' ), . )" />
Note: additional methods in the XslHelper class such as may be of value.
The .NET equivalent:
Sitecore.Data.Fields.LinkField lFld = Sitecore.Context.Item.Fields["link"];
if ( lFld.TargetItem != null ) {
// handle lFld.TargetItem.Fields["path"].Value;
}
InternalLink
Fields of type InternalLink store the full Sitecore path to the selected item, for instance:
/sitecore/media library/PDFs/Subdir/test pdf
The path to the file referenced in the referenced media item can be retrieved with XSL:
<xsl:variable name="mediaitempath" select="sc:fld( 'selecteditem', . )" />
<xsl:variable name="mediaitem" select="sc:item( $mediaitempath, . )" />
<sc:text field="path" select="$mediaitem" />
Or abbreviated:
<sc:text field="path" select=" select="sc:item( sc:fld( 'selecteditem', . ), . )" />
The .NET equivalent:
string mediaItemPath = Sitecore.Context.Item.Fields["selecteditem"].Value;
Sitecore.Data.Items.Item itm = Sitecore.Context.Item.Database.Items[mediItemPath];
itm.Fields["path"].Value;
Or abbreviated:
Sitecore.Context.Item.Database.Items[Sitecore.Context.Item.Fields["selecteditem"].Value].Fields["path"].Value;
File
Fields of type File store the Sitecore path relative to the media library, for instance:
/PDFs/Subdir/test pdf
The media path must be inserted at the beginning of this path to retrieve the media item:
<xsl:variable name="mediaitempath" select="sc:fld( 'selecteditem', . )" />
<xsl:variable name="mediaitemfullpath" select="concat( '/sitecore/media library', sc:fld( 'selecteditem', . ))" />
<xsl:variable name="mediaitem" select="sc:item( $mediaitemfullpath, . )" />
<sc:text field="path" select="$mediaitem" />
Or abbreviated:
<sc:text field="path" select="sc:item( concat( '/sitecore/media library', sc:fld( 'selecteditem', . )), . )" />
The equivalent .NET would generally be a helper method:
public static string GetMediaFilePath( Sitecore.Data.Items.Item itm, string field ) {
string filePath = null;
string mediaPath = itm.Fields[field].Value;
Sitecore.Data.Items.Item mediaItem = itm.Database.Items["/sitecore/media library/" + mediaPath];
if ( mediaItem != null ) {
filePath = mediaItem.Fields["path"].Value;
}
return( filePath );
}
GetMediaFilePath( Sitecore.Context.Item, "selecteditem" );
Prev Next